<?php
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\comm\service\SysToolsLevePoint;
class Sytools extends BasicAdmin
{
	/**
	 * 会员等级规则表的增删改
	 * @return [type] [description]
	 */
	public function level(){
		$this->title = '会员等级规则';
        $get = $this->request->get();
        $db = Db::name('sys_level');
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        $yangshi = config("qiniu");
        $this->assign('yangshi',$yangshi['yangshi']);          //七牛样式后缀
        return parent::_list($db);
	}
	/**
	 * 会员等级增加
	 * @return [type] [description]
	 */
	public function level_add(){
		$this->title = '会员等级规则增加';
		$systoolspoint  = new SysToolsLevePoint();
		$post = $this->request -> post();
		if($post){
			$sle = $systoolspoint  -> add_level($post);
			if($sle){
				 $this->success('恭喜, 数据保存成功!', '');	
			}
			$this->error('不好意思，保存失败!', '');	
		}
		
		return $this->fetch('', []);
	}

	/**
	 * 编辑规则
	 */
	public function level_editor(){
		$this->title = '会员等级规则编辑';
		$systoolspoint  = new SysToolsLevePoint();
		$id = $this->request->get('id');
		
		if (request()->isGet()){
			$lerve =	$systoolspoint ->getlevelbyid($id);
			return $this->fetch('', ['lerve' =>$lerve]);
		}

		if (request()->isPost()){
			$post = $this->request -> post();
			$sle = $systoolspoint  -> editor_level($post);
			if($sle){
				 $this->success('恭喜, 数据保存成功!', '');	
			}
			$this->error('不好意思，保存失败!', '');
		}

	}


/****************************积分***********************************************/
	/**
	 * 会员积分规则表的增删改
	 * @return [type] [description]
	 */
	public function point(){
		$this->title = '会员积分规则';
        $get = $this->request->get();
        $db = Db::name('sys_point');
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        $yangshi = config("qiniu");
        $this->assign('yangshi',$yangshi['yangshi']);          //七牛样式后缀
        return parent::_list($db);
	}

	/**
	 * 积分增加
	 * @return [type] [description]
	 */
	public function point_add(){
		$this->title = '会员等级规则增加';
		$systoolspoint  = new SysToolsLevePoint();
		$post = $this->request -> post();
		if (request()->isPost()){
			$sle = $systoolspoint  -> add_point($post);
			if($sle){
				 $this->success('恭喜, 数据保存成功!', '');	
			}
			$this->error('不好意思，保存失败!', '');	
		}
		return $this->fetch('', []);
	}

	/**
	 * 编辑积分
	 */
	public function point_editor(){
		$this->title = '会员积分编辑';
		$systoolspoint  = new SysToolsLevePoint();
		$id = $this->request->get('id');
		
		if (request()->isGet()){
			$point =	$systoolspoint ->getpointbyid($id);
			return $this->fetch('', ['point' =>$point]);
		}

		if (request()->isPost()){
			$post = $this->request -> post();
			$sle = $systoolspoint  -> editor_point($post);
			if($sle){
				 $this->success('恭喜, 数据保存成功!', '');	
			}
			$this->error('不好意思，保存失败!', '');
		}
	}

/***********************************************轮播图******************/
    /**
     * @Author  lingyun
     * @Desc	网站轮播图
     */
	public function banner(){
        $this->title = '轮播图管理';
        $get = $this->request->get();
        $db = Db::name('banner')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT');


        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.title', "%{$get[$key]}%");
        }
		$db->field('a.*,b.name,b.logo');

        return parent::_list($db);
	}

    /**
     * @Author  lingyun
     * @Desc	添加编辑轮播图
     */
	public function add_edit_banner(){
		$request = request();
		if($request->isPost()){
			$post = input('post.');
			$post['pc_logo'] = trim($post['pc_logo']);
			$post['m_logo'] = trim($post['m_logo']);
			if($post['goods_id'] == ''){
			    $this->error('推荐商品不能为空','');
            }
			if(isset($post['id'])){
				$res = db('banner')->update($post);
				$res!==false?$this->success('保存成功',''):$this->error('保存失败','');
			}else{
				$post['addtime'] = time();
				$res = db('banner')->insert($post);
				$res>0?$this->success('保存成功',''):$this->error('保存失败','');
			}
		}

		$id = input('id');
		$info = db('banner')->where('id',$id)->find();
		$goods = db('goods')->where(['status'=>1,'is_deleted'=>0])->field('logo,id,name')->select();
		$this->assign('info',$info);
		$this->assign('goods',$goods);

		return $this->fetch('add_edit_banner');
	}

    /**
     * @Author  lingyun
     * @Desc	删除轮播图
     */
	public function del_banner(){
		$id = input('post.id');
		$res = db('banner')->where('id','in',$id)->delete();
		$res>0?$this->success('删除成功',''):$this->error('删除失败','');
	}

    /**
     * @Author  lingyun
     * @Desc	广告
     */
	public function advert(){
        $this->title = '广告管理';
        $get = $this->request->get();
        $db = Db::name('advert')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT');


        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.title', "%{$get[$key]}%");
        }
        $db->field('a.*,b.name,b.hot_pc_banner');

        return parent::_list($db);
	}

    /**
     * @Author  lingyun
     * @Desc	编辑 -- 添加广告
     */
	public function add_edit_advert(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $post['pc_logo'] = trim($post['pc_logo']);
            $post['m_logo'] = trim($post['m_logo']);
            if($post['goods_id'] == ''){
            $this->error('请选择推荐商品','');
            }
            if(isset($post['id'])){
                $res = db('advert')->update($post);
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }else{
                $post['addtime'] = time();
                $count = db('advert')->where('adp',$post['adp'])->count();
                if($count >= 1){
                    $this->error('该广告位置已经存在广告，请勿重复设置','');
                }
                $res = db('advert')->insert($post);
                $res>0?$this->success('保存成功',''):$this->error('保存失败','');
            }
        }

        $id = input('id');
        $info = db('advert')->where('id',$id)->find();
        $goods = db('goods')->where(['status'=>1,'is_deleted'=>0])->field('logo,id,name')->select();
        $this->assign('info',$info);
        $this->assign('goods',$goods);

        return $this->fetch('add_edit_advert');
	}

    /**
     * @Author  lingyun
     * @Desc	删除广告
     */
	public function del_advert(){
        $id = input('post.id');
        $res = db('advert')->where('id','in',$id)->delete();
        $res>0?$this->success('删除成功',''):$this->error('删除失败','');
	}


//    /**
//     * @Author  lingyun
//     * @Desc	网站轮播图
//     */
//	public function banner_position(){
//        $this->title = '广告位管理';
//        $get = $this->request->get();
//        $db = Db::name('banner_position')->where('is_deleted',0);
//        foreach (['title'] as $key) {
//            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
//        }
//        return parent::_list($db);
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	添加 -- 编辑轮播图
//     */
//	public function add_edit_banner(){
//		$id = input('id');		//轮播图id
//		$request = request();
//		if($request->isPost()){
//			$post = input('post.');
//            if(!isset($post['id'])){
//				$post['addtime'] = time();
//				$res = db('banner_position')->data($post)->insert();
//				db('banner_position')->where('id',$res)->update(['bap_id'=>$res]);
//				$res>0?$this->success('保存成功',''):$this->error('保存失败','');
//			}else{
//				$res = db('banner_position')->data($post)->update();
//				$res!==false?$this->success('保存成功',''):$this->error('保存失败','');
//			}
//		}
//		$where_banner['id'] = ['eq',$id];
//		$where_goods = [
//			'is_online'=>1,
//			'status'=>1,
//			'is_deleted'=>0
//		];
//		$banner = db('banner_position')->where($where_banner)->find();
//		$goods = db('goods')->where($where_goods)->field('name,logo,id')->select();
//		$this->assign('banner',$banner);
//		$this->assign('goods',$goods);
//		return $this->fetch();
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	删除轮播图
//     */
//	public function del_banner(){
//        $table = 'BannerPosition';
//        if (DataService::update($table)) {
//            $this->success("删除成功！", '');
//        }
//        $this->error("删除失败，请稍候再试！");
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	设置轮播图片的状态
//     */
//	public function set_banner_status(){
//		$post = input('post.');
//		$res = db('banner_position')->where('id',$post['id'])->update([$post['field']=>$post['value']]);
//		$res!==false?$this->success('更新成功',''):$this->error('更新失败',1);
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	广告位管理
//     */
//	public function advert_position(){
//        $this->title = '广告位管理';
//        $get = $this->request->get();
//        $db = Db::name('advert_position')->where('is_deleted',0);
//        foreach (['title'] as $key) {
//            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
//        }
//        return parent::_list($db);
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	添加 -- 编辑广告位
//     */
//	public function add_edit_advert(){
//		$request = request();
//		if($request->isPost()){
//			$post = input('post.');
//			if(empty($post['id'])){
//				$post['addtime'] = time();
//				$res = db('advert_position')->data($post)->insert();
//				$c_id = db('advert_position')->getLastInsID();		//获取最新的自增id
//				db('advert_position')->where('id',$c_id)->update(['adp_id'=>$c_id]);
//				$res>0?$this->success('保存成功',''):$this->error('保存失败','');
//			}else{
//				$res = db('advert_position')->data($post)->update();
//				$res!==false?$this->success('保存成功',''):$this->error('保存失败','');
//			}
//		}
//		//广告位id
//		$id = input('id');
//		$where_advert_position['id'] = ['eq',$id];
//		$info = db('advert_position')->where($where_advert_position)->find();
//		$this->assign('info',$info);
//		return $this->fetch();
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	删除广告位
//     */
//	public function set_advert_status(){
//		$post = input('post.');
//		$res = db('advert_position')->where('id',$post['id'])->update(['status'=>$post['value']]);
//		$res!==false?$this->success('设置成功',''):$this->error('设置失败','');
//	}
//
//    /**
//     * @Author  lingyun
//     * @Desc	删除广告位
//     */
//	public function del_advert(){
//        $table = 'AdvertPosition';
//        if (DataService::update($table)) {
//            $this->success("删除成功！", '');
//        }
//        $this->error("删除失败，请稍候再试！");
//	}

}

	
